package com.zhoujian.netty;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.nio.charset.StandardCharsets;

/**
 * @author zhoujian
 * @version 1.0
 * @date 2023/9/17 18:21
 */
public class MyChannerHandler extends ChannelInboundHandlerAdapter {
    //重写channelRead方法，用于处理收到的数据，并返回客户端
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        ByteBuf byteBuf = (ByteBuf) msg;
        System.out.println("服务端已经收到了消息---->" + byteBuf.toString(StandardCharsets.UTF_8));

        //可以通过cts获取channel
        ctx.channel().writeAndFlush(Unpooled.copiedBuffer("hello Client".getBytes(StandardCharsets.UTF_8)));
    }
    //重写方法，用于处理异常

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        super.exceptionCaught(ctx, cause);
    }
}
